# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import json


class DaohangPipeline(object):
    def process_item(self, item, spider):
        return item


class JsonWriterPipeline(object):
    """保存为.jl文件"""

    def __init__(self):
        self.file = open('./sina.jl', 'w', encoding='utf-8')  # 打开sina.jl文件

    def open_spider(self, spider):  # 方法名均为固定写法写法
        pass

    def close_spider(self, spider):  # 关闭sina.jl文件
        self.file.close()

    def process_item(self, item, spider):
        line = json.dumps(dict(item), ensure_ascii=False) + "\n"  # 将item转换为字典，然后序列化为json格式的字符串
        self.file.write(line)
        return item
